package com.electric.qrapp.webView

import android.content.Context
import androidx.lifecycle.MutableLiveData
import com.electric.qrapp.ui.base.vm.BaseViewModel
import com.electric.qrapp.utils.function.SingleLiveData
import com.electric.qrapp.utils.function.startLocation
import com.electric.qrapp.utils.system.showToast
import com.electric.qrapp.utils.view.LoadingUtils
import com.huantansheng.easyphotos.models.album.entity.Photo
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject


/**
 *@author created by WA03675
 *@date  2022-03-16 15:12
 *@des
 */
@HiltViewModel
class WebViewViewModel @Inject constructor(
    private val repository: WebViewRepository
) : BaseViewModel() {

    private var exitTime = 0L
    val vmLoc = SingleLiveData<String>()
    val vmAlbum = SingleLiveData<String>()

    val albumPermissionListener = SingleLiveData<Boolean>()
    val filePermissionListener = SingleLiveData<Boolean>()


    fun finishAct(): Boolean {
        if ((System.currentTimeMillis() - exitTime) > 2000) {
            showToast(msg = "再按一次退出应用")
            exitTime = System.currentTimeMillis();
            return false
        }
        return true
    }

    /**
     *定位
     * @param context
     */
    fun getLocation(context: Context) {
        startLocation(context, vmLoc)
    }


    /**
     *文件压缩
     * @param context
     * @param list
     */
    fun compressFile(context: Context, list: List<Photo>?) {

        list?.map {
            it.path
        }?.run {
            repository.compressFile(context, this, vmAlbum, LoadingUtils())
        }
    }

}


